<?php
defined ('_ALLOW') or die ('Access denied');
class phaply {
	var $id;
	var $title;
	var $order;
	var $nums;
	
	function Doc_danh_sach () {
		return $this->Fill ("");
	}
	function CountFill ($strwhere) {
		global $csdl;
		$strsql = "SELECT COUNT(*) as count FROM phaply";
		if ($strwhere != "")
			$strsql .= " WHERE " . $strwhere;
		$rowsdb = $csdl->Truyvan($strsql);
		$rowdb = mysql_fetch_array($rowsdb,MYSQL_ASSOC);
		$count = $rowdb['count'];
		return $count;
	}
	function Fill ($strwhere) {
		global $csdl;
		global $lang;
		$strsql = "SELECT * FROM phaply";
		if ($strwhere!="") {
			$strsql .= " WHERE " . $strwhere;
		}
		$strsql .= " ORDER BY iorder DESC, id";

		$rowsdb = $csdl->Truyvan ($strsql);
		$i=0;
		$this->nums=0;
		if ($rowsdb==false) return null;
		while ($rowdb = mysql_fetch_array($rowsdb,MYSQL_ASSOC)) {
			$rowarray[$i] = $this->Khoi_tao($rowdb);
			$i++;
		}
		if ($i==0) return null;
		$this->nums = $i;
		return $rowarray;
	}
	function Doc ($id) {
		$rowarray = $this->Fill ("id=$id");
		if ($rowarray==null)
			return null;
		else
			return $rowarray[0];
	}
	function CheckSearch ($keyword) {
		if ($keyword=="") return false;
		$strwhere = "";
		$strwhere .= "(";
		$strwhere .= "lower(title) LIKE lower('%" . $keyword . "%') ";
		
		$strwhere .= ")";
		return $this->Fill($strwhere);
	}
	
	function Khoi_tao ($rowdb) {
		$kq = new phaply;
		$kq->id = $rowdb['id'];
		$kq->title = $rowdb['title'];
		$kq->order = $rowdb['iorder'];
		return $kq;
	}
	function Xoa () {
		checkpermission("grantcat",$this->catid) or die ('Access denied');
		global $csdl;
		$csdl->Xoa("phaply","id=$this->id");
	}
	function DocForm () {
		$this->id = $_REQUEST['id'];
		$this->title = $_REQUEST['title'];
	}
	function GetMaxOrder () {
		$strwhere = "iorder>=all";
		$strwhere .= " (SELECT iorder FROM phaply)";
		$rowarray = $this->Fill ($strwhere, 1, 0);
		if ($rowarray==null) return 0;
		return $rowarray[0]->order;
	}
	function ChangeOrder ($neworder) {
		checkpermission("grantcat",$this->catid) or die ('Access denied');
		global $csdl;
		$strsql = "UPDATE `phaply` SET ";
		$strsql .= "`iorder` = $neworder ";
		$strsql .= "WHERE `id`=$this->id";
		$csdl->Ghi($strsql);
	}
	function Ghi () {
		checkpermission("grantcat",$this->catid) or die ('Access denied');
		global $csdl;
		if ($this->id > -1) {
			$strsql = "UPDATE `phaply` SET ";
			$strsql .= "`title`='$this->title' ";
			$strsql .= "WHERE `id`=$this->id";
		}
		else {
			$neworder = $this->GetMaxOrder() + 1;
			$idadmin = $_SESSION['_idadmin'];
			$strsql = "INSERT INTO phaply (`title`, `iorder`) ";
			$strsql .= "VALUES ('$this->title', $neworder)";
		}
		$csdl->Ghi($strsql);
	}
};
global $objphaply; $objphaply = new phaply;
?>